﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraTabbedMdi;
using DevExpress.XtraReports.UI;
using System.IO;

namespace TCCT_CTC_CSDLDangVienV1
{
    #region EUtils

    /// <summary>
    /// EXIM Utilities
    /// </summary>
    public static class EUtils
    {
        #region Properties

        #region MainForm variables

        #region MainForm

        private static RibbonForm _mainForm;

        /// <summary>
        /// Gets or sets the main form of the application
        /// </summary>
        /// <value>The main form.</value>
        public static RibbonForm MainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }

        #endregion MainForm

        #region ButtonImageCollection

        private static ImageCollection _buttonImageCollection;

        /// <summary>
        /// Gets or sets the button image collection.
        /// </summary>
        /// <value>The button image collection.</value>
        public static ImageCollection ButtonImageCollection
        {
            get { return _buttonImageCollection; }
            set { _buttonImageCollection = value; }
        }

        public static class ButtonIndex
        {
            public const int INSERT = 0;
            public const int UPDATE = 1;
            public const int DELETE = 2;
            public const int REPORT = 3;
            public const int SAVE = 4;
            public const int CANCEL = 5;
            public const int REFRESH = 6;
            public const int CLOSE = 7;
            public const int SEARCH = 8;
            public const int RESET = 9;
            public const int EXPORT_EXCEL = 10;
            public const int ACCEPT = 11;
            //public const int INSERT = 0;
        }

        #endregion ButtonImageCollection

        #region TabMdiManager

        private static XtraTabbedMdiManager _tabMdiManager = null;

        /// <summary>
        /// Gets or sets the tab MDI manager on the Main Form
        /// </summary>
        /// <value>The tab MDI manager.</value>
        public static XtraTabbedMdiManager TabMdiManager
        {
            get { return _tabMdiManager; }
            set { _tabMdiManager = value; }
        }

        #endregion TabMdiManager

        #region MainRebbon

        private static RibbonControl _mainRibbon;

        public static RibbonControl MainRibbon
        {
            get { return _mainRibbon; }
            set { _mainRibbon = value; }
        }

        #endregion MainRebbon

        #region ProgreesToolBar

        public static DevExpress.XtraEditors.MarqueeProgressBarControl _progreesToolBar = null;

        /// <summary>
        /// Gets or sets the progrees tool bar.
        /// </summary>
        /// <value>The progrees tool bar.</value>
        public static DevExpress.XtraEditors.MarqueeProgressBarControl ProgreesToolBar
        {
            get { return _progreesToolBar; }
            set { _progreesToolBar = value; }
        }

        #endregion ProgreesToolBar

        #region ProgreesButton

        public static DevExpress.XtraBars.BarButtonItem _progreesButton = null;

        /// <summary>
        /// Gets or sets the progrees button.
        /// </summary>
        /// <value>The progrees button.</value>
        public static DevExpress.XtraBars.BarButtonItem ProgreesButton
        {
            get { return _progreesButton; }
            set { _progreesButton = value; }
        }

        #endregion ProgreesButton

        #region TabbedMdiCheck

        private static DevExpress.XtraBars.BarCheckItem _tabbedMdiCheck = null;

        public static DevExpress.XtraBars.BarCheckItem TabbedMdiCheck
        {
            get
            {
                return _tabbedMdiCheck;
            }
            set
            {
                _tabbedMdiCheck = value;
            }
        }

        #endregion TabbedMdiCheck

        #region ButtonKey

        private static string _buttonKey;

        public static string ButtonKey
        {
            get { return _buttonKey; }
            set { _buttonKey = value; }
        }

        #endregion ButtonKey

        #endregion MainForm variables

        #region Dialogs variables

        private static object _returnedObject = null;

        /// <summary>
        /// Khong duoc thay doi gia tri cua Thuoc tinh nay - Chi su dung cho button "Cap Nhat Chuc Nang" (DONGLD)
        /// </summary>
        public static bool DialogModel = true;

        #endregion Dialogs variables

        #endregion Properties

        #region Controls



        #region LoadControlDialog

        private static void CaptureReturnedObject(object returnedObject)
        {
            _returnedObject = returnedObject;
        }

        public static object LoadControlDialog(frmBase control, DialogSize dialogSize, bool resizeable)
        {
            control.ReturnObjectEvent += CaptureReturnedObject;

            XtraForm form = new XtraForm();
            form.Name = control.Name;

            #region Set Size

            if (dialogSize == DialogSize.Small)
            {
                form.Width = 400;
                form.Height = 300;
            }
            else if (dialogSize == DialogSize.Medium)
            {
                form.Width = 600;
                form.Height = 450;
            }
            else if (dialogSize == DialogSize.Large)
            {
                form.Width = 800;
                form.Height = 600;
            }
            else if (dialogSize == DialogSize.Custom)
            {
                form.Width = control.Width + 2;
                form.Height = control.Height + 30;
            }

            #endregion Set Size

            form.Controls.Add(control);
            DevExpress.XtraEditors.SimpleButton CancelButton = new SimpleButton();

            CancelButton.DialogResult = DialogResult.Cancel;
            CancelButton.TabIndex = 2000;
            CancelButton.TabStop = false;
            form.Controls.Add(CancelButton);
            form.CancelButton = CancelButton;

            control.Dock = DockStyle.Fill;
            if (resizeable)
            {
                form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
                form.MaximizeBox = true;
            }
            else
            {
                form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                form.MaximizeBox = false;
            }

            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowIcon = true;
            form.ShowInTaskbar = false;

            if (DialogModel)
                form.ShowDialog(MainForm);
            else
                form.Show(MainForm);

            return _returnedObject;
        }

        /// <summary>
        /// Loads the control dialog.
        /// </summary>
        /// <param name="control">The control.</param>
        public static object LoadControlDialog(frmBase control, DialogSize dialogSize)
        {
            return LoadControlDialog(control, dialogSize, true);
        }

        public static void LoadControlDialog(frmBase control, InputBoxSize inputBoxSize)
        {
            control.Height = (int)inputBoxSize;
            LoadControlDialog(control, DialogSize.Custom);
        }

        #endregion LoadControlDialog


        #region LoadControlMdi

        /// <summary>
        /// Loads the control MDI.
        /// </summary>
        /// <param name="control">The control.</param>
        public static void LoadControlMdi(frmBase control)
        {

            if (control.ParentForm != null && control.ParentForm.Visible)
            {
                control.ParentForm.Activate();
                return;
            }

            if (EUtils.TabMdiManager.MdiParent == null && EUtils.TabbedMdiCheck.Checked)
                EUtils.TabMdiManager.MdiParent = EUtils.MainForm;
            XtraForm form = new XtraForm();
            form.Name = control.Name;

            form.Width = control.Width;

            form.Controls.Add(control);
            control.Dock = DockStyle.Fill;
            //form.Dock = DockStyle.Fill;
            form.MdiParent = MainForm;
            form.Show();


            form.ShowIcon = true;
            form.Activate();

            if (EUtils.TabMdiManager.MdiParent != null)
            {

                //  EUtils.TabMdiManager.SelectedPage.Image = tempImage;
            }
        }


        #endregion LoadControlMdi

        #endregion Controls

        #region ConvertToCheckState
        public static CheckState ConvertToCheckState(string sudung)
        {
            sudung = sudung.ToLower();
            if (sudung.Equals("true")) return CheckState.Checked;
            if (sudung.Equals("false"))
            {
                return CheckState.Unchecked;
            }
            else
                return CheckState.Indeterminate;

        }
        #endregion ConvertToCheckState



        #region Reports

        #region CreatParamTable

        public static DataTable CreatParamTable()
        {
            DataTable dt = new DataTable();

            for (int i = 1; i <= 20; i++)
            {
                dt.Columns.Add("Column" + i);
            }

            dt.AcceptChanges();

            dt.TableName = "DanhSachParam";

            return dt;
        }

        #endregion CreatParamTable

        /// <summary>
        /// Shows the report MDI.
        /// Thường dùng cho dạng báo chỉ có Danh sách kết quả và tham số
        /// </summary>
        /// <param name="reportLayoutName">Name of the report layout.</param>
        /// <param name="DetailTable">The detail table.</param>
        /// <param name="lstParam">The arr param. tối đa 20 tham số</param>
        /// <param name="reportTitle">The report title.</param>
        public static void ShowReport(string reportLayoutName, DataTable DetailTable, List<DevExpress.XtraReports.Parameters.Parameter> lstParam, string reportTitle, OpenFormType openFormType)
        {
            DevExpress.XtraReports.UI.XtraReport report = new XtraReport();

            DataSet reportDS = new DataSet();


            report.LoadLayout(Application.StartupPath + "\\Reports\\" + reportLayoutName + ".repx");
            MyCommon.DBHelper.SetDataTableName(DetailTable, report.DataMember.ToString());
            MyCommon.DBHelper.AddTableToDataSet(reportDS, DetailTable);

            for (int i = 0; i < lstParam.Count; i++)
            {
                report.Parameters.Add(lstParam[i]);
            }

            report.DataAdapter = null;
            report.DataSource = reportDS;
            if (reportTitle.Length == 0)
                reportTitle = "Báo cáo";

            TCCT_CTC_CSDLDangVienV1.Reports.ReportMain reportViewer = new TCCT_CTC_CSDLDangVienV1.Reports.ReportMain();
            reportViewer.LoadReport(report);

            if (openFormType == OpenFormType.MDI)
                LoadControlMdi(reportViewer);
            else
                LoadControlDialog(reportViewer, DialogSize.Custom);
        }
        public static void RenderReport(string reportLayoutName, DataTable DetailTable, List<DevExpress.XtraReports.Parameters.Parameter> lstParam, Reports.ReportMain rptMain, DevExpress.XtraReports.UI.XtraReport report)
        {

            DataSet reportDS = new DataSet();

            DataTable paramDT = CreatParamTable();

            DataRow drNew = paramDT.NewRow();

            for (int i = 0; i < lstParam.Count; i++)
            {
                drNew[i] = (double)lstParam[i].Value;
            }

            paramDT.Rows.Add(drNew);
            paramDT.AcceptChanges();


            report.LoadLayout(Application.StartupPath + "\\Reports\\" + reportLayoutName + ".repx");
            MyCommon.DBHelper.SetDataTableName(DetailTable, report.DataMember.ToString());
            MyCommon.DBHelper.AddTableToDataSet(reportDS, DetailTable);
            MyCommon.DBHelper.AddTableToDataSet(reportDS, paramDT);



            report.DataAdapter = null;
            report.DataSource = reportDS;

            rptMain.LoadReport(report);
        }
        public static void ShowReport(string reportLayoutName, DataSet reportDS, List<DevExpress.XtraReports.Parameters.Parameter> lstParam, string reportTitle, OpenFormType openFormType)
        {
            DevExpress.XtraReports.UI.XtraReport report = new XtraReport();



            report.LoadLayout(Application.StartupPath + "\\Reports\\" + reportLayoutName + ".repx");

            for (int i = 0; i < lstParam.Count; i++)
            {
                report.Parameters.Add(lstParam[i]);
            }
            report.DataAdapter = null;
            report.DataSource = reportDS;
            if (reportTitle.Length == 0)
                reportTitle = "Báo cáo";

            TCCT_CTC_CSDLDangVienV1.Reports.ReportMain reportViewer = new TCCT_CTC_CSDLDangVienV1.Reports.ReportMain();
            reportViewer.LoadReport(report);
            if (openFormType == OpenFormType.MDI)
                LoadControlMdi(reportViewer);
            else
                LoadControlDialog(reportViewer, DialogSize.Custom);
        }


        public static void ShowReportMdi(XtraReport report, DataSet dtSource, string reportName)
        {
            report.DataAdapter = null;
            report.DataSource = dtSource;
            if (reportName.Length == 0)
                reportName = "Báo cáo";

            TCCT_CTC_CSDLDangVienV1.Reports.ReportMain reportViewer = new TCCT_CTC_CSDLDangVienV1.Reports.ReportMain();
            reportViewer.LoadReport(report);
            LoadControlMdi(reportViewer);
        }


        public static void ShowReport(XtraReport report, DataSet dtSource, string reportName,
            bool loadLayout)
        {
            //Khoi tao Report Viewer
            TCCT_CTC_CSDLDangVienV1.Reports.ReportMain preview;

            preview = new TCCT_CTC_CSDLDangVienV1.Reports.ReportMain();

            //Set Data for Report
            report.DataAdapter = null;
            report.DataSource = dtSource;

            //Load layout if neeeded
            if (loadLayout)
            {
                //report.LoadLayout("D:\\abc.repx");
            }

            //Set Default Report Name
            if (reportName.Length == 0)
                reportName = "Báo cáo";

            //Load Report into Report Viewer
            preview.LoadReport(report);

            //Show Report Viewer as MDI Form
            EUtils.LoadControlMdi(preview);
        }

        #endregion Reports

        #region HStrings

        public static class HStrings
        {
            public static class HoTen
            {
                public static string ReturnHo(string hoTen)
                {
                    string ho = string.Empty;
                    if (hoTen.LastIndexOf(" ") > 0)
                    {
                        ho = hoTen.Substring(0, hoTen.LastIndexOf(" "));
                    }
                    return ho;
                }
                public static string ReturnTen(string hoTen)
                {
                    string ten = hoTen;

                    if (hoTen.LastIndexOf(" ") > 0)
                    {
                        ten = hoTen.Substring(hoTen.LastIndexOf(" ") + 1);
                    }
                    return ten;
                }
            }
            public static string[] ReturnArr(string str, char split)
            {
                string[] returnArr;
                if (str.Length == 0)
                    return null;
                else
                {
                    returnArr = str.Split(split);
                }
                return returnArr;
            }

            #region AppendConditionString


            /// <summary>
            /// Appends the condition string.
            /// </summary>
            /// <param name="S1">The s1.</param>
            /// <param name="S2">The s2.</param>
            /// <returns></returns>
            public static string AppendConditionString(ref string S1, string S2)
            {
                if (S1.Length > 0)
                    S1 += " & ";
                S1 += S2;
                return S1;
            }

            #endregion AppendConditionString
        }

        #endregion HStrings
    }

    #endregion EUtils

    #region SysInfo

    /// <summary>
    /// Get system information
    /// </summary>
    public class SysInfo
    {

        public string IPAddress
        {
            get
            {
                try
                {
                    return Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
                }
                catch
                {
                    return "127.0.0.1";
                }
            }
        }

        public string MachineName
        {
            get
            {
                return Environment.MachineName;
            }
        }

        public string SystemInfo
        {
            get
            {
                string systemInfo = string.Empty;


                return systemInfo;
            }
        }


    }

    #endregion SysInfo

    #region DialogSize

    /// <summary>
    /// Kích thước của cửa sổ dialog
    /// </summary>
    public enum DialogSize
    {
        /// <summary>
        /// Cỡ nhỏ 400x300
        /// </summary>
        Small,
        /// <summary>
        /// Cỡ vừa 600x450
        /// </summary>
        Medium,
        /// <summary>
        /// Cỡ lớn 800x600
        /// </summary>
        Large,
        /// <summary>
        /// Do control quyết định
        /// </summary>
        Custom
    }
    public enum InputBoxSize
    {
        /// <summary>
        /// Height=135
        /// </summary>
        Size1 = 110,
        /// <summary>
        /// Height=180
        /// </summary>
        Size2 = 155,
        /// <summary>
        /// Height=225
        /// </summary>
        Size3 = 200
    }

    #endregion DialogSize

    #region OpenFormType

    public enum OpenFormType
    {
        MDI,
        Dialog = 200
    }
    #endregion OpenFormType
}
